package com.example.hotel_lxy.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.EventBusBean;
import com.example.hotel_lxy.bean.RvZeroDataBean;
import com.example.hotel_lxy.bean.ZeroPointTabBean;
import com.example.hotel_lxy.bean.ZeroPointTabRvBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.dialog.HistoryOrderDialog;
import com.example.hotel_lxy.ui.dialog.ReserveDialog;
import com.example.hotel_lxy.ui.dialog.ZeroInformationDialog;
import com.example.hotel_lxy.ui.main.CalendarActivity;
import com.example.hotel_lxy.utils.DatePhotoUtils;
import com.example.hotel_lxy.utils.DateUtils;
import com.example.hotel_lxy.utils.JurisdictionUtils;
import com.example.hotel_lxy.utils.SPUtils;
import com.google.android.material.tabs.TabLayout;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 零点首页预定
 */
@Route(path = "/main/ZeroPointActivity")
public class ZeroPointActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text_date)
    TextView textDate;
    @BindView(R.id.tabs_pending)
    TabLayout tabsPending;
    @BindView(R.id.rv_tab_list)
    RecyclerView rvTabList;
    @BindView(R.id.rv_data_list)
    RecyclerView rvDataList;
    @BindView(R.id.last_time)
    TextView lastTime;
    @BindView(R.id.next_time)
    TextView nextTime;
    @BindView(R.id.line_yl)
    LinearLayout lineYl;
    @BindView(R.id.line_bz)
    LinearLayout lineBz;
    private String year, mMonth, mDay, mWay;
    private List<ZeroPointTabBean.DataBean.AreaBean> mTitles = new ArrayList<>();
    private List<ZeroPointTabRvBean> mRvList = new ArrayList<>();
    private String qid = "";
    private String pid = "";
    RBaseAdapter<ZeroPointTabRvBean> rBaseAdapter;
    private int type = 1;//判断是否时之前的
    private String addzero;
    public static void start() {
        ARouter.getInstance().build(ARouterPath.getZeroPointActivity())
                .navigation();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_zero_point;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void doWork() {
        super.doWork();
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);  //事件的注册
            EventBus.getDefault().register(this);  //事件的注册
        }else{
            EventBus.getDefault().register(this);  //事件的注册
        }
       addzero = SPUtils.get(ZeroPointActivity.this,"addzero","").toString();
        showDate();
        showTab();
        // showRvDataDataList(qid, pid);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lastTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lineYl.setVisibility(View.GONE);
                lineBz.setVisibility(View.GONE);
                showLast();
            }
        });
        nextTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNext();
            }
        });
        textDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CalendarActivity.start(3);
            }
        });

    }

    private void showNext() {
        int monthOfDay1 = DatePhotoUtils.getMonthOfDay(Integer.valueOf(year), Integer.valueOf(mMonth));
        if ((Integer.valueOf(mDay) + 1) > monthOfDay1) {
            if ((Integer.valueOf(mMonth) + 1) > 12) {
                int y = (Integer.valueOf(year) + 1);
                year = y + "";
                mMonth = "01";
            } else {
                int m = (Integer.valueOf(mMonth) + 1);
                if(m<10){
                    mMonth = "0"+m + "";
                }else{
                    mMonth = m + "";
                }
            }
            mDay = "01";
        } else {
            int d = (Integer.valueOf(mDay) + 1);
            if(d<10){
                mDay = "0"+d + "";
            }else{
                mDay = d + "";
            }

        }

        Log.e("TAG",year+" 下 "+mMonth+" "+mDay+"  "+qid+"  "+pid);
        showDateTime();
        showRvDataDataList(qid, pid);
    }

    private void showLast() {
        type = 1;
        if ((Integer.valueOf(mDay) - 1) == 0) {
            if ((Integer.valueOf(mMonth) - 1) == 0) {
                int y = (Integer.valueOf(year) - 1);
                year = y + "";
                mMonth = "12";
            } else {
                int m = (Integer.valueOf(mMonth) - 1);
                if(m<10){
                    mMonth = "0"+m + "";
                }else{
                    mMonth = m + "";
                }
            }
            int monthOfDay = DatePhotoUtils.getMonthOfDay(Integer.valueOf(year), Integer.valueOf(mMonth));
            mDay = monthOfDay + "";
        } else {
            int d = (Integer.valueOf(mDay) - 1);
            if(d<10){
                mDay = "0"+d + "";
            }else{
                mDay = d + "";
            }
        }

        Log.e("TAG",year+" 上 "+mMonth+" "+mDay+"  "+qid+"  "+pid);
        showDateTime();
        showRvDataDataList(qid, pid);
    }

    private void showDateTime() {
        mWay = DatePhotoUtils.getWeek1(year + "-" + mMonth + "-" + mDay);
        SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy-MM-dd");
        Date curDate =  new Date(System.currentTimeMillis());
        String[] split = formatter.format(curDate).split("-");
        String yearS =split[0];
        String month = split[1];
        String day = split[2];
        long timeGetTime = DatePhotoUtils.dateToStamp(yearS + "-" + month + "-" + day+" 23:59:59");
        long s = DatePhotoUtils.dateToStamp(year + "-" + mMonth + "-" + mDay+" 23:59:59");
        if (s < timeGetTime) {
            type = 2;
            lineYl.setVisibility(View.GONE);
            lineBz.setVisibility(View.GONE);
        } else if (s == timeGetTime) {
            type = 1;
            lineYl.setVisibility(View.VISIBLE);
            lineBz.setVisibility(View.VISIBLE);
        } else {
            type = 3;
            lineYl.setVisibility(View.VISIBLE);
            lineBz.setVisibility(View.VISIBLE);
        }

        textDate.setText(mMonth + "月" + mDay + "日  周" + mWay);
    }

    private void showRvDataDataList(String qid, String pid) {
        OkHttpUtils.post()
                .url(Api.POST_ZEROINDEX)
                .addHeader("flag", SPUtils.get(ZeroPointActivity.this, "token", "").toString())
                .addParams("year", year + "")
                .addParams("month",   mMonth+"")
                .addParams("day",  mDay+"")
                .addParams("qid", qid)
                .addParams("pid", pid)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "零点首页1onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "零点首页1onResponse   " + response);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(ZeroPointActivity.this,"token","");
                            Intent intent = new Intent(ZeroPointActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        RvZeroDataBean rvZeroDataBean = JsonUtils.parseByGson(response, RvZeroDataBean.class);
                        if (beanBean.getCode().equals("0")) {
                            rvDataList.setLayoutManager(new LinearLayoutManager(ZeroPointActivity.this));
                            RBaseAdapter<RvZeroDataBean.DataBean.ListBean> rvZeroDataBeanRBaseAdapter = new RBaseAdapter<RvZeroDataBean.DataBean.ListBean>(
                                    ZeroPointActivity.this, R.layout.item_zero_data_title_activity, rvZeroDataBean.getData().getList()) {
                                @Override
                                protected void fillItem(RViewHolder holder, RvZeroDataBean.DataBean.ListBean items, int position) {
                                    RecyclerView recyclerView = holder.itemView.findViewById(R.id.recyclerView);
                                    TextView text_title = holder.itemView.findViewById(R.id.text_title);
                                    text_title.setText("" + items.getName());
                                    if(items.getElegant()==null){

                                        return;
                                    }
                                    recyclerView.setLayoutManager(new GridLayoutManager(ZeroPointActivity.this, 4));
                                    RBaseAdapter<RvZeroDataBean.DataBean.ListBean.ElegantBean> rBaseAdapter = new RBaseAdapter<RvZeroDataBean.DataBean.ListBean.ElegantBean>(
                                            ZeroPointActivity.this, R.layout.item_zero_data_activity, items.getElegant()) {
                                        @Override
                                        protected void fillItem(RViewHolder holder, RvZeroDataBean.DataBean.ListBean.ElegantBean item, int position) {
                                            TextView text_title = holder.itemView.findViewById(R.id.text_title);
                                            LinearLayout line_noon = holder.itemView.findViewById(R.id.line_noon);
                                            LinearLayout line_afternoon = holder.itemView.findViewById(R.id.line_afternoon);
                                            TextView text_number = holder.itemView.findViewById(R.id.text_number);
                                            //午
                                            TextView text_noon_name = holder.itemView.findViewById(R.id.text_noon_name);
                                            TextView text_noon_director = holder.itemView.findViewById(R.id.text_noon_director);
                                            TextView text_noon_over = holder.itemView.findViewById(R.id.text_noon_over);
                                            text_title.setText("" + item.getName());
                                            text_number.setText("" + item.getPid() + "");
                                            if (item.getNoon().getName() != null) {
                                                text_noon_director.setVisibility(View.VISIBLE);
                                                if (item.getNoon().getOver() == 1) {//翻台
                                                    if (type == 2) {
                                                        text_noon_over.setVisibility(View.VISIBLE);
                                                    } else {
                                                        if (item.getNoon().getContent() != null && !item.getNoon().getContent().equals("")) {//备注
                                                            text_noon_over.setVisibility(View.VISIBLE);
                                                            text_noon_over.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_radius15_ff0006));
                                                        } else {
                                                            text_noon_over.setVisibility(View.GONE);
                                                        }
                                                    }
                                                    String mobile1 = item.getNoon().getMobile() + "";
                                                    String mobile;
                                                    if (mobile1.length() > 4) {
                                                        mobile = mobile1.substring(mobile1.length()-4, mobile1.length());
                                                    } else {
                                                        mobile = mobile1;
                                                    }
                                                    String substring = item.getNoon().getName().substring(0, 1);
                                                    if(item.getAfternoon().getSex()==1){
                                                        substring=substring+"先生";
                                                    }else{
                                                        substring=substring+"女士";
                                                    }
                                                    text_noon_name.setText("" + substring + mobile);
                                                    text_noon_director.setText("【" + item.getNoon().getDirector() + "】");
                                                    if (item.getNoon().getStatus() == 1) {//预定
                                                        line_noon.setBackgroundDrawable(getResources().getDrawable(R.color.color_FFCA00));
                                                    } else {//入座
                                                        line_noon.setBackgroundDrawable(getResources().getDrawable(R.color.color_15D37D));
                                                    }
                                                } else {
                                                    if (type != 2) {
                                                        if (item.getNoon().getContent() != null && !item.getNoon().getContent().equals("")) {//备注
                                                            text_noon_over.setVisibility(View.VISIBLE);
                                                            text_noon_over.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_radius15_ff0006));
                                                        } else {
                                                            text_noon_over.setVisibility(View.GONE);
                                                        }
                                                    } else {
                                                        text_noon_over.setVisibility(View.GONE);
                                                    }
                                                    String mobile1 = item.getNoon().getMobile() + "";
                                                    String mobile;
                                                    if (mobile1.length() > 4) {
                                                        mobile = mobile1.substring(mobile1.length()-4, mobile1.length());
                                                    } else {
                                                        mobile = mobile1;
                                                    }
                                                    String substring = item.getNoon().getName().substring(0, 1);
                                                    if(item.getNoon().getSex()==1){
                                                        substring=substring+"先生";
                                                    }else{
                                                        substring=substring+"女士";
                                                    }
                                                    text_noon_name.setText("" + substring + mobile);
                                                    text_noon_director.setText("【" + item.getNoon().getDirector() + "】");
                                                    if (item.getNoon().getStatus() == 1) {//预定
                                                        line_noon.setBackgroundDrawable(getResources().getDrawable(R.color.color_FFCA00));
                                                    } else {//入座
                                                        line_noon.setBackgroundDrawable(getResources().getDrawable(R.color.color_15D37D));
                                                    }
                                                }

                                            }
                                            else {
                                                line_noon.setBackgroundDrawable(getResources().getDrawable(R.color.color_C4C2C2));
                                                text_noon_name.setText("午");
                                                text_noon_director.setVisibility(View.GONE);
                                                text_noon_over.setVisibility(View.GONE);
                                            }
                                            //晚
                                            TextView text_afternoon_name = holder.itemView.findViewById(R.id.text_afternoon_name);
                                            TextView text_afternoon_director = holder.itemView.findViewById(R.id.text_afternoon_director);
                                            TextView text_afternoon_over = holder.itemView.findViewById(R.id.text_afternoon_over);
                                            if (item.getAfternoon().getName() != null) {
                                                text_afternoon_director.setVisibility(View.VISIBLE);
                                                if (item.getAfternoon().getOver() == 1) {//翻台
                                                    if (type == 2) {
                                                        text_afternoon_over.setVisibility(View.VISIBLE);
                                                    } else {
                                                        if (item.getAfternoon().getContent() != null && !item.getAfternoon().getContent().equals("")) {//备注
                                                            text_afternoon_over.setVisibility(View.VISIBLE);
                                                            text_afternoon_over.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_radius15_ff0006));
                                                        } else {
                                                            text_afternoon_over.setVisibility(View.GONE);
                                                        }
                                                    }
                                                    String mobile1 = item.getAfternoon().getMobile() + "";
                                                    String mobile;
                                                    if (mobile1.length() > 4) {
                                                        mobile = mobile1.substring(mobile1.length()-4, mobile1.length());
                                                    } else {
                                                        mobile = mobile1 + "";
                                                    }
                                                    String substring = item.getAfternoon().getName().substring(0, 1);
                                                    if(item.getAfternoon().getSex()==1){
                                                        substring=substring+"先生";
                                                    }else{
                                                        substring=substring+"女士";
                                                    }
                                                    text_afternoon_name.setText("" + substring + mobile);
                                                    text_afternoon_director.setText("【" + item.getAfternoon().getDirector() + "】");
                                                    if (item.getAfternoon().getStatus() == 1) {//预定
                                                        line_afternoon.setBackgroundDrawable(getResources().getDrawable(R.color.color_FFCA00));
                                                    } else {//入座
                                                        line_afternoon.setBackgroundDrawable(getResources().getDrawable(R.color.color_15D37D));
                                                    }
                                                } else {
                                                    if (type != 2) {
                                                        if (item.getAfternoon().getContent() != null && !item.getAfternoon().getContent().equals("")) {//备注
                                                            text_afternoon_over.setVisibility(View.VISIBLE);
                                                            text_afternoon_over.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_radius15_ff0006));
                                                        } else {
                                                            text_afternoon_over.setVisibility(View.GONE);
                                                        }
                                                    } else {
                                                        text_afternoon_over.setVisibility(View.GONE);
                                                    }
                                                    String mobile1 = item.getAfternoon().getMobile() + "";
                                                    String mobile;
                                                    if (mobile1.length() > 4) {
                                                        mobile = mobile1.substring(mobile1.length()-4, mobile1.length());
                                                    } else {
                                                        mobile = mobile1 + "";
                                                    }

                                                    String substring = item.getAfternoon().getName().substring(0, 1);
                                                    if(item.getAfternoon().getSex()==1){
                                                        substring=substring+"先生";
                                                    }else{
                                                        substring=substring+"女士";
                                                    }
                                                    text_afternoon_name.setText("" + substring + mobile);
                                                    text_afternoon_director.setText("【" + item.getAfternoon().getDirector() + "】");
                                                    if (item.getAfternoon().getStatus() == 1) {//预定
                                                        line_afternoon.setBackgroundDrawable(getResources().getDrawable(R.color.color_FFCA00));
                                                    } else {//入座
                                                        line_afternoon.setBackgroundDrawable(getResources().getDrawable(R.color.color_15D37D));
                                                    }
                                                }

                                            }
                                            else {
                                                line_afternoon.setBackgroundDrawable(getResources().getDrawable(R.color.color_C4C2C2));
                                                text_afternoon_name.setText("晚");
                                                text_afternoon_director.setVisibility(View.GONE);
                                                text_afternoon_over.setVisibility(View.GONE);
                                            }
                                            //早
                                            line_noon.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    boolean jcklingdianck = JurisdictionUtils.getJcklingdianck(ZeroPointActivity.this);
                                                    if(jcklingdianck){
                                                        ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                        return;
                                                    }
                                                    if (type == 2) {//历史
                                                        if(item.getNoon().getName()!=null){
                                                            boolean jcklingdianck1 = JurisdictionUtils.getLdchakanqb(ZeroPointActivity.this);
                                                            if(jcklingdianck1){
                                                                HistoryOrderDialog.show(ZeroPointActivity.this, null, year,mMonth,mDay,
                                                                        items.getId()+"",item.getId()+"","1"  );//历史订单
                                                            }else{
                                                                boolean getuserid = JurisdictionUtils.getuserid(ZeroPointActivity.this,
                                                                        item.getNoon().getDirector_id() + "");
                                                                if(getuserid){
                                                                    HistoryOrderDialog.show(ZeroPointActivity.this, null, year,mMonth,mDay,
                                                                            items.getId()+"",item.getId()+"","1"  );//历史订单
                                                                }else{
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                            }
                                                        }else{
                                                            ToastUtils.longToast(ZeroPointActivity.this,"过去的时间不能预定哦！");
                                                        }
                                                    }
                                                    else if(type==3){
                                                        if (item.getNoon().getStatus() == 1) {//预定

                                                            boolean jcklingdianck1 = JurisdictionUtils.getLdchakanqb(ZeroPointActivity.this);
                                                            if(jcklingdianck1){
                                                                ReserveDialog.show(ZeroPointActivity.this, null,item.getNoon().getId() + "",
                                                                        year + "-" + mMonth + "-" + mDay+"晚餐"+items.getName().replace("雅间","")+item.getName()
                                                                        , type,4);//零点预定信息 入座
                                                            }else{
                                                                boolean getuserid = JurisdictionUtils.getuserid(ZeroPointActivity.this, item.getNoon().getDirector_id() + "");
                                                                if(getuserid){
                                                                    ReserveDialog.show(ZeroPointActivity.this, null,item.getNoon().getId() + "",
                                                                            year + "-" + mMonth + "-" + mDay+"晚餐"+items.getName().replace("雅间","")+item.getName()
                                                                            , type,4);//零点预定信息 入座
                                                                }else{
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                            }

                                                        } else if (item.getNoon().getStatus() == 2) {

                                                            boolean jcklingdianck1 = JurisdictionUtils.getLdchakanqb(ZeroPointActivity.this);
                                                            if(jcklingdianck1){
                                                                ZeroInformationDialog.show(ZeroPointActivity.this, null,item.getNoon().getId() + "",
                                                                        year + "-" + mMonth + "-" +
                                                                        mDay+"晚餐"+items.getName().replace("雅间","")+item.getName()); //零点预定信息 翻台
                                                            }else{
                                                                boolean getuserid = JurisdictionUtils.getuserid(ZeroPointActivity.this, item.getNoon().getDirector_id() + "");
                                                                if(getuserid){
                                                                    ZeroInformationDialog.show(ZeroPointActivity.this, null,item.getNoon().getId() + "",
                                                                            year + "-" + mMonth + "-" +
                                                                            mDay+"晚餐"+items.getName().replace("雅间","")+item.getName()); //零点预定信息 翻台
                                                                }else{
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                            }


                                                        } else {
                                                            boolean addzero = JurisdictionUtils.getAddzero(ZeroPointActivity.this);
                                                            if(!addzero){
                                                                ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                return;
                                                            }
                                                           ZeroPointReserveActivity.start(item.getId() + "", item.getName(), year + "-" + mMonth + "-" + mDay, "  周" + mWay, "1");// 零点预定页
                                                        }
                                                    } else {
                                                        if (item.getNoon().getStatus() == 1) {//预定


                                                            boolean jcklingdianck1 = JurisdictionUtils.getLdchakanqb(ZeroPointActivity.this);
                                                            if(jcklingdianck1){
                                                                ReserveDialog.show(ZeroPointActivity.this, null,item.getNoon().getId() + "",
                                                                        year + "-" + mMonth + "-" + mDay+"晚餐"+items.getName().replace("雅间","")+item.getName()
                                                                        , type,4);//零点预定信息  入座
                                                            }else{
                                                                boolean getuserid = JurisdictionUtils.getuserid(ZeroPointActivity.this, item.getNoon().getDirector_id() + "");
                                                                if(getuserid){
                                                                    ReserveDialog.show(ZeroPointActivity.this, null,item.getNoon().getId() + "",
                                                                            year + "-" + mMonth + "-" + mDay+"晚餐"+items.getName().replace("雅间","")+item.getName()
                                                                            , type,4);//零点预定信息  入座
                                                                }else{
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                            }


                                                        } else if (item.getNoon().getStatus() == 2) {

                                                            boolean jcklingdianck1 = JurisdictionUtils.getLdchakanqb(ZeroPointActivity.this);
                                                            if(jcklingdianck1){
                                                                ZeroInformationDialog.show(ZeroPointActivity.this, null,item.getNoon().getId() + "",
                                                                        year + "-" + mMonth + "-" + mDay+"晚餐"+items.getName().replace("雅间","")+
                                                                                item.getName()); //零点预定信息 翻台
                                                            }else{
                                                                boolean getuserid = JurisdictionUtils.getuserid(ZeroPointActivity.this, item.getNoon().getDirector_id() + "");
                                                                if(getuserid){
                                                                    ZeroInformationDialog.show(ZeroPointActivity.this, null,item.getNoon().getId() + "",
                                                                            year + "-" + mMonth + "-" + mDay+"晚餐"+items.getName().replace("雅间","")+
                                                                                    item.getName()); //零点预定信息 翻台
                                                                }else{
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                            }


                                                        } else {
                                                            SimpleDateFormat  formatter  =  new  SimpleDateFormat   ("HH:mm");
                                                            Date curDate =  new Date(System.currentTimeMillis());
                                                            String time = "00:00-" + formatter.format(curDate);
                                                            String[] two = time.split("-");
                                                            String[] second = two[1].split(":");
                                                            int secondTotalMinute = Integer.valueOf(second[0]) * 60 + Integer.valueOf(second[1]);
                                                            if (secondTotalMinute < 960) {
                                                                boolean addzero = JurisdictionUtils.getAddzero(ZeroPointActivity.this);
                                                                if(!addzero){
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                                ZeroPointReserveActivity.start(item.getId() + "", item.getName(), year + "-" + mMonth + "-" + mDay, "  周" + mWay, "1");// 零点预定页
                                                            } else {
                                                                ToastUtils.show(ZeroPointActivity.this, "已过预定时间段");
                                                            }
                                                        }
                                                    }
                                                }
                                            });
                                            //晚
                                            line_afternoon.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    boolean jcklingdianck = JurisdictionUtils.getJcklingdianck(ZeroPointActivity.this);
                                                    if(jcklingdianck){
                                                        ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                        return;
                                                    }
                                                    if (type == 2) {//历史
                                                        if(item.getAfternoon().getName()!=null){
                                                            boolean jcklingdianck1 = JurisdictionUtils.getLdchakanqb(ZeroPointActivity.this);
                                                            if(jcklingdianck1){
                                                                HistoryOrderDialog.show(ZeroPointActivity.this, null, year,mMonth,mDay,
                                                                        items.getId()+"",item.getId()+"","2");//历史订单
                                                            }else{
                                                                boolean getuserid = JurisdictionUtils.getuserid(ZeroPointActivity.this,
                                                                        item.getAfternoon().getDirector_id() + "");
                                                                if(getuserid){
                                                                    HistoryOrderDialog.show(ZeroPointActivity.this, null, year,mMonth,mDay,
                                                                            items.getId()+"",item.getId()+"","2");//历史订单
                                                                }else{
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                            }

                                                        }else{
                                                            ToastUtils.longToast(ZeroPointActivity.this,"过去的时间不能预定哦！");
                                                        }
                                                    }
                                                    else if(type==3){
                                                        if (item.getAfternoon().getStatus() == 1) {//预定
                                                            boolean jcklingdianck1 = JurisdictionUtils.getLdchakanqb(ZeroPointActivity.this);
                                                            if(jcklingdianck1){
                                                                ReserveDialog.show(ZeroPointActivity.this, null,item.getAfternoon().getId() + "",
                                                                        year + "-" + mMonth + "-" + mDay+"晚餐"+items.getName().replace("雅间","")+item.getName()
                                                                        , type,5);//零点预定信息
                                                            }else{
                                                                boolean getuserid = JurisdictionUtils.getuserid(ZeroPointActivity.this,
                                                                        item.getAfternoon().getDirector_id() + "");
                                                                if(getuserid){
                                                                    ReserveDialog.show(ZeroPointActivity.this, null,item.getAfternoon().getId() + "",
                                                                            year + "-" + mMonth + "-" + mDay+"晚餐"+items.getName().replace("雅间","")+item.getName()
                                                                            , type,5);//零点预定信息
                                                                }else{
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                            }


                                                        } else if (item.getAfternoon().getStatus() == 2) {
                                                            boolean jcklingdianck1 = JurisdictionUtils.getLdchakanqb(ZeroPointActivity.this);
                                                            if(jcklingdianck1){
                                                                ZeroInformationDialog.show(ZeroPointActivity.this, null,
                                                                        item.getAfternoon().getId() + "",year + "-" + mMonth + "-" + mDay+"晚餐"+
                                                                                items.getName().replace("雅间","")+item.getName()); //零点预定信息
                                                            }else{
                                                                boolean getuserid = JurisdictionUtils.getuserid(ZeroPointActivity.this,
                                                                        item.getAfternoon().getDirector_id() + "");
                                                                if(getuserid){
                                                                    ZeroInformationDialog.show(ZeroPointActivity.this, null,
                                                                            item.getAfternoon().getId() + "",year + "-" + mMonth + "-" + mDay+"晚餐"+
                                                                                    items.getName().replace("雅间","")+item.getName()); //零点预定信息
                                                                }else{
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                            }


                                                        } else {
                                                            boolean addzero = JurisdictionUtils.getAddzero(ZeroPointActivity.this);
                                                            if(!addzero){
                                                                ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                return;
                                                            }
                                                            ZeroPointReserveActivity.start(item.getId() + "", item.getName(), year + "-" + mMonth + "-" + mDay, "  周" + mWay, "2");// 零点预定页
                                                        }
                                                    }
                                                    else {
                                                        if (item.getAfternoon().getStatus() == 1) {//预定

                                                            boolean jcklingdianck1 = JurisdictionUtils.getLdchakanqb(ZeroPointActivity.this);
                                                            if(jcklingdianck1){

                                                                ReserveDialog.show(ZeroPointActivity.this, null,item.getAfternoon().getId() + "",
                                                                        year + "-" + mMonth + "-" + mDay+"晚餐"+items.getName().replace("雅间","")+item.getName()
                                                                        , type,5);//零点预定信息
                                                            }else{
                                                                boolean getuserid = JurisdictionUtils.getuserid(ZeroPointActivity.this,
                                                                        item.getAfternoon().getDirector_id() + "");
                                                                if(getuserid){

                                                                    ReserveDialog.show(ZeroPointActivity.this, null,item.getAfternoon().getId() + "",
                                                                            year + "-" + mMonth + "-" + mDay+"晚餐"+items.getName().replace("雅间","")+item.getName()
                                                                            , type,5);//零点预定信息
                                                                }else{
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                            }

                                                        } else if (item.getAfternoon().getStatus() == 2) {
                                                            boolean jcklingdianck1 = JurisdictionUtils.getLdchakanqb(ZeroPointActivity.this);
                                                            if(jcklingdianck1){
                                                                ZeroInformationDialog.show(ZeroPointActivity.this, null,
                                                                        item.getAfternoon().getId() + "",year + "-" + mMonth + "-" + mDay+
                                                                                "晚餐"+items.getName().replace("雅间","")
                                                                                +item.getName()); //零点预定信息
                                                            }else{
                                                                boolean getuserid = JurisdictionUtils.getuserid(ZeroPointActivity.this,
                                                                        item.getAfternoon().getDirector_id() + "");
                                                                if(getuserid){
                                                                    ZeroInformationDialog.show(ZeroPointActivity.this, null,
                                                                            item.getAfternoon().getId() + "",year + "-" + mMonth + "-" + mDay+
                                                                                    "晚餐"+items.getName().replace("雅间","")
                                                                                    +item.getName()); //零点预定信息
                                                                }else{
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                            }

                                                        } else {
                                                            SimpleDateFormat  formatter  =  new  SimpleDateFormat   ("HH:mm");
                                                            Date curDate =  new Date(System.currentTimeMillis());
                                                            String time = "00:00-" + formatter.format(curDate);
                                                            String[] two = time.split("-");
                                                            String[] second = two[1].split(":");
                                                            int secondTotalMinute = Integer.valueOf(second[0]) * 60 + Integer.valueOf(second[1]);
                                                            if (secondTotalMinute < 1439) {
                                                                boolean addzero = JurisdictionUtils.getAddzero(ZeroPointActivity.this);
                                                                if(!addzero){
                                                                    ToastUtils.longToast(ZeroPointActivity.this,"当前账号没有操作权限哦");
                                                                    return;
                                                                }
                                                                ZeroPointReserveActivity.start(item.getId() + "", item.getName(), year + "-" + mMonth + "-" + mDay, "  周" + mWay, "2");// 零点预定页
                                                            } else {
                                                                ToastUtils.show(ZeroPointActivity.this, "已过预定时间段");
                                                            }
                                                        }
                                                    }
                                                }
                                            });
                                        }

                                        @Override
                                        protected int getViewType(RvZeroDataBean.DataBean.ListBean.ElegantBean item, int position) {
                                            return 0;
                                        }
                                    };
                                    recyclerView.setAdapter(rBaseAdapter);
                                }

                                @Override
                                protected int getViewType(RvZeroDataBean.DataBean.ListBean item, int position) {
                                    return 0;
                                }
                            };
                            rvDataList.setAdapter(rvZeroDataBeanRBaseAdapter);
                        }
                        else{
                            ToastUtils.shortToast(ZeroPointActivity.this,""+beanBean.getMsg());
                        }
                    }
                });


    }

    private void showRvDataList() {
        LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
        layoutmanager.setOrientation(LinearLayoutManager.HORIZONTAL);//列表横向显示
        rvTabList.setLayoutManager(layoutmanager);
        rBaseAdapter = new RBaseAdapter<ZeroPointTabRvBean>(this,
                R.layout.item_zero_activity, mRvList) {
            @Override
            protected void fillItem(RViewHolder holder, ZeroPointTabRvBean item, int position) {
                TextView text_number = holder.itemView.findViewById(R.id.text_number);
                LinearLayout line = holder.itemView.findViewById(R.id.line);
                text_number.setText(item.getName());
                if (item.isaBoolean()) {
                    line.setBackgroundDrawable(getResources().getDrawable(R.mipmap.icon_top_item_bg));
                } else {
                    line.setBackgroundDrawable(getResources().getDrawable(R.mipmap.icon_zero_bg));
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(item.isaBoolean()){
                            for (ZeroPointTabRvBean data : mRvList) {
                                data.setaBoolean(false);
                            }
                            mRvList.get(position).setaBoolean(false);
                            pid =  "";
                        }else{
                            for (ZeroPointTabRvBean data : mRvList) {
                                data.setaBoolean(false);
                            }
                            pid = item.getId() + "";
                            mRvList.get(position).setaBoolean(true);
                        }
                        notifyDataSetChanged();
                        showRvDataDataList(qid, pid);
                    }
                });


            }

            @Override
            protected int getViewType(ZeroPointTabRvBean item, int position) {
                return 0;
            }
        };
        rvTabList.setAdapter(rBaseAdapter);
    }

    private void showTab() {
        OkHttpUtils.post()
                .url(Api.POST_ZEROELEGANT)
                .addHeader("flag", SPUtils.get(ZeroPointActivity.this, "token", "").toString())
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "零点首页TABonError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "零点首页TABonResponse   " + response);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(ZeroPointActivity.this,"token","");
                            Intent intent = new Intent(ZeroPointActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        ZeroPointTabBean zeroPointTabBean = JsonUtils.parseByGson(response, ZeroPointTabBean.class);
                        if (beanBean.getCode().equals("0")) {
                            mTitles = zeroPointTabBean.getData().getArea();
                            for (int i = 0; i < zeroPointTabBean.getData().getList().size(); i++) {
                                List<ZeroPointTabBean.DataBean.ListBean> list = zeroPointTabBean.getData().getList();
                                ZeroPointTabRvBean zeroPointTabRvBean = new ZeroPointTabRvBean();
                                zeroPointTabRvBean.setId(list.get(i).getId()+"");
                                zeroPointTabRvBean.setName(list.get(i).getName());
                                zeroPointTabRvBean.setaBoolean(false);
                                mRvList.add(zeroPointTabRvBean);
                            }
                            tabsPending.addTab(tabsPending.newTab().setText("全部"));
                            for (ZeroPointTabBean.DataBean.AreaBean title : mTitles) {
                                tabsPending.addTab(tabsPending.newTab().setText(title.getName()));
                            }
                            tabsPending.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                                @Override
                                public void onTabSelected(TabLayout.Tab tab) {
                                    int position = tab.getPosition();
                                   if(position==0){
                                       qid =  "";
                                   } else{
                                       qid = mTitles.get((position-1)).getId() + "";
                                   }
                                    showRvDataDataList(qid, pid);
//                                    for (ZeroPointTabRvBean data : mRvList) {
//                                        data.setaBoolean(false);
//                                    }
                                    rBaseAdapter.notifyDataSetChanged();
                                }

                                @Override
                                public void onTabUnselected(TabLayout.Tab tab) {

                                }

                                @Override
                                public void onTabReselected(TabLayout.Tab tab) {

                                }
                            });
                            showRvDataList();
                        }
                    }
                });
    }

    private void showDate() {
        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR) + "";
       int iq= c.get(Calendar.DAY_OF_MONTH);
       if(iq<10){
           mDay="0"+iq;
       }else{
           mDay=""+iq;
       }
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        int i = (c.get(Calendar.MONTH) + 1);
        if (i < 10) {
            mMonth = "0" + i;
        } else {
            mMonth = "" + i;
        }
        if ("1".equals(mWay)) {
            mWay = "日";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        textDate.setText(mMonth + "月" + mDay + "日  周" + mWay);
    }


    @Override
    protected void onResume() {
        super.onResume();
        showRvDataDataList(qid, pid);
    }

    // 普通事件的处理
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleEvent(EventBusBean carrier) {
        if (carrier != null) {
            if (carrier.getType() == 3) {
                String[] two = carrier.getStarttime().split("-");
                year = two[0] + "";
                mMonth = two[1] + "";
                mDay = two[2] + "";
                String replace = carrier.getStarttime().replace("-", "-");
                int dayofWeek = DateUtils.getDayofWeek(replace);
                if (dayofWeek == 1) {
                    mWay = "日";
                } else if (dayofWeek == 2) {
                    mWay = "一";
                } else if (dayofWeek == 3) {
                    mWay = "二";
                } else if (dayofWeek == 4) {
                    mWay = "三";
                } else if (dayofWeek == 5) {
                    mWay = "四";
                } else if (dayofWeek == 6) {
                    mWay = "五";
                } else if (dayofWeek == 7) {
                    mWay = "六";
                }
                showDateTime();
                showRvDataDataList(qid, pid);
            }
            else if(carrier.getType()==9){
                showRvDataDataList(qid, pid);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this); //解除注册
    }
}